#########################################################################   
#                         INFO                                          #   
#########################################################################  

# PROJECT: Gender Stereotypes, Political Leadership, and Voting Behavior in Tunisia
# PURPOSE: Cleaning the survey data
# CREATED: September 2019 by Alexandra Blackman & Marlette Jackson
# INPUTS:  femalepol_bjka.xlsx; femalepol_yg.xlsx
# OUTPUTS: tun_bjka.xlsx; tun_yg.xlsx

#########################################################################   
#                        SETUP                                          #   
######################################################################### 

rm(list = ls()) 
setwd("~/Dropbox/Voters and Female Politicians/Political Behavior")

#########################################################################   
#                        LOAD PACKAGES                                  #   
######################################################################### 

need <- c("doBy","foreign", "lubridate","extrafont", "car", "plyr",
          "reshape", "openxlsx", "tidyr", "dplyr", "ggplot2", "ggthemes",
          "RColorBrewer", "rms", "lmtest", "sandwich", "msm",
          "stargazer","texreg","cowplot","plm","margins","prediction",
          "gplots","ggpubr", "sjmisc","sjPlot","gtable","cjoint","cregg",
          "scales","multiwayvcov") # packages needed

# Note: You may have to install.packages("data.table", type = "binary") manually 
# to load package car 

have <- need %in% rownames(installed.packages()) 
if(any(!have)) install.packages(need[!have]) 

invisible(lapply(need, library, character.only=T)) 


#########################################################################   
#                       LOAD DATA                                       #   
#########################################################################   

## LOAD BJKA HOUSEHOLD SURVEY (JULY 2017)
tun_bjka <- read.xlsx("femalepol_bjka.xlsx")

## LOAD YOUGOV ONLINE SURVEY (APRIL 2019)
tun_yg <- read.xlsx("femalepol_yg.xlsx")


#########################################################################   
#                       CLEAN VARIABLES                                 #   
#########################################################################  

#########################################################################   
#             DEFINE PATRIARCHAL/EGALITARIAN TYPES                      #   
#########################################################################   

# BJKA: create patriarchal type and index measures
# Agree or Strong Agree with more than 1 == partriarchal
tun_bjka$patr_pol <- ifelse(tun_bjka$att_women_att_women_1=="Strongly disagree",0,
                            ifelse(tun_bjka$att_women_att_women_1=="Somewhat disagree",0,
                                   ifelse(tun_bjka$att_women_att_women_1=="Somewhat agree",1,
                                          ifelse(tun_bjka$att_women_att_women_1=="Strongly agree",1,NA))))
table(tun_bjka$patr_pol)

tun_bjka$patr_edu <- ifelse(tun_bjka$att_women_att_women_2=="Strongly disagree",0,
                            ifelse(tun_bjka$att_women_att_women_2=="Somewhat disagree",0,
                                   ifelse(tun_bjka$att_women_att_women_2=="Somewhat agree",1,
                                          ifelse(tun_bjka$att_women_att_women_2=="Strongly agree",1,NA))))
table(tun_bjka$patr_edu)

tun_bjka$patr_jobs <- ifelse(tun_bjka$att_women_att_women_3=="Strongly disagree",0,
                             ifelse(tun_bjka$att_women_att_women_3=="Somewhat disagree",0,
                                    ifelse(tun_bjka$att_women_att_women_3=="Somewhat agree",1,
                                           ifelse(tun_bjka$att_women_att_women_3=="Strongly agree",1,NA))))
table(tun_bjka$patr_jobs)

# eliminate complete NAs from BJKA data   
tun_bjka$missing <- ifelse(is.na(tun_bjka$patr_pol)==T &
                             is.na(tun_bjka$patr_edu)==T &
                             is.na(tun_bjka$patr_jobs)==T, 1, 0)

tun_bjka_na <- tun_bjka %>% dplyr::filter(missing==0)

tun_bjka_na$patr_index <- rowSums(tun_bjka_na[,c("patr_pol", "patr_edu", "patr_jobs")], na.rm=TRUE)
table(tun_bjka_na$patr_index)

tun_bjka_na$patriarchal <- ifelse(tun_bjka_na$patr_pol==1 & tun_bjka_na$patr_edu==1 |
                                    tun_bjka_na$patr_pol==1 & tun_bjka_na$patr_jobs==1 |
                                    tun_bjka_na$patr_edu==1 & tun_bjka_na$patr_jobs==1,1,0)

table(tun_bjka_na$patriarchal)

450/(620+450) #42 Percent of the sample is patriarchal

tun_bjka_na$type <- ifelse(tun_bjka_na$patriarchal==1,"Patriarchal","Egalitarian")
table(tun_bjka_na$type)

tun_bjka_na$responseid <- as.character(tun_bjka_na$responseid)


# YouGov: create patriarchal type and index measures
# Agree or Strong Agree with more than 1 == partriarchal
tun_yg$patr_pol <- ifelse(tun_yg$QS1_women_leaders=="Strongly disagree",0,
                          ifelse(tun_yg$QS1_women_leaders=="Disagree",0,
                                 ifelse(tun_yg$QS1_women_leaders=="Agree",1,
                                        ifelse(tun_yg$QS1_women_leaders=="Strongly agree",1,NA))))
table(tun_yg$patr_pol)

tun_yg$patr_edu <- ifelse(tun_yg$QS1_women_edu=="Strongly disagree",0,
                          ifelse(tun_yg$QS1_women_edu=="Disagree",0,
                                 ifelse(tun_yg$QS1_women_edu=="Agree",1,
                                        ifelse(tun_yg$QS1_women_edu=="Strongly agree",1,NA))))
table(tun_yg$patr_edu)

tun_yg$patr_jobs <- ifelse(tun_yg$QS1_women_jobs=="Strongly disagree",0,
                           ifelse(tun_yg$QS1_women_jobs=="Disagree",0,
                                  ifelse(tun_yg$QS1_women_jobs=="Agree",1,
                                         ifelse(tun_yg$QS1_women_jobs=="Strongly agree",1,NA))))
table(tun_yg$patr_jobs)

tun_yg$patr_index <- rowSums(tun_yg[,c("patr_pol", "patr_edu", "patr_jobs")], na.rm=TRUE)
table(tun_yg$patr_index)


tun_yg$patriarchal <- ifelse(tun_yg$patr_pol==1 & tun_yg$patr_edu==1 |
                               tun_yg$patr_pol==1 & tun_yg$patr_jobs==1 |
                               tun_yg$patr_edu==1 & tun_yg$patr_jobs==1,1,0)

table(tun_yg$patriarchal)

176/(398+176) #30.7 Percent of the sample is patriarchal

tun_yg$type <- ifelse(tun_yg$patriarchal==1,"Patriarchal","Egalitarian")
table(tun_yg$type)

tun_yg$respondentid <- as.factor(tun_yg$RecordNo)


#########################################################################   
#             RECODE THE VIGNETTE OUTCOME VARIABLES                     #   
#########################################################################   

# BJKA: recode vote_choice_mer to be 0-4

table(tun_bjka_na$vote_choice_mer)
tun_bjka_na$support <- ifelse(tun_bjka_na$vote_choice_mer==5,4,
                           ifelse(tun_bjka_na$vote_choice_mer==4,3,
                                  ifelse(tun_bjka_na$vote_choice_mer==3,2,
                                         ifelse(tun_bjka_na$vote_choice_mer==2,1,
                                                ifelse(tun_bjka_na$vote_choice_mer==1,0,NA)))))

table(tun_bjka_na$support)


# YouGov: recode QS7_vig1 & QS7_vig2 to be 0-4

table(tun_yg$QS7_vig1)
table(tun_yg$QS7_vig2)
tun_yg$choice1 <- ifelse(tun_yg$QS7_vig1=="Extremely likely",4,
                         ifelse(tun_yg$QS7_vig1=="Very likely",3,
                                ifelse(tun_yg$QS7_vig1=="Moderately likely",2,
                                       ifelse(tun_yg$QS7_vig1=="Slightly likely",1,
                                              ifelse(tun_yg$QS7_vig1=="I would not vote for $herhim1",0,NA)))))

tun_yg$choice2 <- ifelse(tun_yg$QS7_vig2=="Extremely likely",4,
                         ifelse(tun_yg$QS7_vig2=="Very likely",3,
                                ifelse(tun_yg$QS7_vig2=="Moderately likely",2,
                                       ifelse(tun_yg$QS7_vig2=="Slightly likely",1,
                                              ifelse(tun_yg$QS7_vig2=="I would not vote for $herhim2",0,NA)))))

table(tun_yg$choice1)
table(tun_yg$choice2)

# Create columns for all possible treatments with the outcome

tun_yg$woman_control <- ifelse(tun_yg$vignette1=="miriam control",tun_yg$choice1,
                               ifelse(tun_yg$vignette2=="miriam control",tun_yg$choice2,NA))

tun_yg$man_control <- ifelse(tun_yg$vignette1=="ahmed control",tun_yg$choice1,
                             ifelse(tun_yg$vignette2=="ahmed control",tun_yg$choice2,NA))

tun_yg$woman_security <- ifelse(tun_yg$vignette1=="miriam security",tun_yg$choice1,
                                ifelse(tun_yg$vignette2=="miriam security",tun_yg$choice2,NA))

tun_yg$man_security <- ifelse(tun_yg$vignette1=="ahmed security",tun_yg$choice1,
                              ifelse(tun_yg$vignette2=="ahmed security",tun_yg$choice2,NA))

#########################################################################   
#                      create gov/del variables                         #   
#########################################################################

# BJKA: create location variable (10 delegations)
# loc_3 = delegation
tun_bjka_na$del_en <- factor(tun_bjka_na$loc_3)
table(tun_bjka_na$del_en)

# YouGov: create location variable (16 cities/regions)
tun_yg$city <- as.character(tun_yg$city)
tun_yg$city_en <- factor(tun_yg$city)
table(tun_yg$city_en)

#########################################################################   
#                      create religion variables                        #   
#########################################################################

# BJKA: Create indicator variable for go to mosque once a day or more (attend_daily)
tun_bjka_na$attend_daily <- ifelse(tun_bjka_na$prayer_attend == "Once a day" | 
                                     tun_bjka_na$prayer_attend == "Several times a day", 1, 
                                   ifelse(tun_bjka_na$prayer_attend == "Refuse to answer" | 
                                            tun_bjka_na$prayer_attend == "Don't Know", NA, 0))
table(tun_bjka_na$attend_daily)

# YouGov: Create indicator variable for go to mosque once a day or more (attend_daily)
tun_yg$attend_daily <- ifelse(tun_yg$QS8_rel1 == "Once a day" | 
                                tun_yg$QS8_rel1 == "Several times a day", 1, 
                              ifelse(tun_yg$QS8_rel1 == "Not Applicable", NA, 0))
table(tun_yg$attend_daily)


# BJKA: Create indicator variable for religion of great importance (relig_great_imp)
tun_bjka_na$relig_great_imp <- ifelse(tun_bjka_na$relig_import == "Great importance", 1, 
                                      ifelse(tun_bjka_na$relig_import == "Refuse to answer" | 
                                               tun_bjka_na$relig_import == "Don't Know", NA, 0))

table(tun_bjka_na$relig_great_imp)

# YouGov: Create indicator variable for religion of great importance (relig_great_imp)
tun_yg$relig_great_imp <- ifelse(tun_yg$QS9_rel2 == "Great importance", 1, 
                                 ifelse(tun_yg$QS9_rel2 == "Refuse to answer" | 
                                          tun_yg$QS9_rel2 == "Don't Know", NA, 0))

table(tun_yg$relig_great_imp)

#########################################################################   
#               create demographic variables                            #   
#########################################################################

# BJKA: Sex (resp_fem)
tun_bjka_na$resp_fem <- ifelse(tun_bjka_na$female=="Female",1,0)

# YouGov: Sex (resp_fem)
tun_yg$resp_fem <- ifelse(tun_yg$gender=="Female",1,0)

## BJKA: Age (age)
tun_bjka_na$resp_age <- as.numeric(tun_bjka_na$age_4_text)

tun_bjka_na$age <- NA
tun_bjka_na$age[tun_bjka_na$resp_age>=(18) & tun_bjka_na$resp_age<(30)]<-"18 to 29"
tun_bjka_na$age[tun_bjka_na$resp_age>=(30) & tun_bjka_na$resp_age<(40)]<-"30 to 39"
tun_bjka_na$age[tun_bjka_na$resp_age>=(40) & tun_bjka_na$resp_age<(99)]<-"40+"
table(tun_bjka_na$age)

## YouGov: Age (age)
tun_yg$age <- tun_yg$vage2

## BJKA: Finished Secondary or higher (educ_high)
tun_bjka_na$educ_high <- ifelse(tun_bjka_na$educ_level == "Secondary" | 
                                  tun_bjka_na$educ_level == "BA" | 
                                  tun_bjka_na$educ_level == "MA or higher", 1, 
                                ifelse(tun_bjka_na$educ_level == "Refuse to answer" | 
                                         tun_bjka_na$educ_level == "Don't Know", NA, 0))
table(tun_bjka_na$educ_high)

## YouGov: Finished Secondary or higher (educ_high)
tun_yg$educ_high <- ifelse(tun_yg$education == "Secondary school" | 
                             tun_yg$education == "Vocational college education (e.g. to qualify as an electrician, nurse)" | 
                             tun_yg$education == "University first degree (e.g. BA, BSc)" | 
                             tun_yg$education == "University higher degree (Masters, MBA, PhD)" | 
                             tun_yg$education == "Professional higher education (e.g. to qualify as a lawyer, accountant)", 
                           1, 0)

table(tun_yg$educ_high)

## BJKA: Married/Widowed (resp_mar_wid)
tun_bjka_na$resp_mar_wid <- ifelse(tun_bjka_na$mar_status=="Married" |
                                     tun_bjka_na$mar_status=="Widowed",1,0)

table(tun_bjka_na$resp_mar_wid)

## YouGov: Married/Widowed (resp_mar_wid)
tun_yg$resp_mar_wid <- ifelse(tun_yg$maritalstatus=="Married with Children" |
                                tun_yg$maritalstatus=="Married without Children" |
                                tun_yg$maritalstatus=="Widowed",1,0)

table(tun_yg$resp_mar_wid)

# BJKA: Most recent election (2014) -- voting behavior (vote_yes)
tun_bjka_na$vote_yes <- ifelse(tun_bjka_na$vote_2014 == "Yes", 1, 0)
table(tun_bjka_na$vote_yes)

# YouGov: Most recent election (2018) -- voting behavior (vote_yes)
tun_yg$vote_yes <- ifelse(tun_yg$QS4_vote_mun18 == "Yes", 1,0)
table(tun_yg$vote_yes)

# BJKA: Employment (employ_status)
tun_bjka_na$employ_status <- ifelse(tun_bjka_na$employ1 == "Yes", "Employed", "Unemployed/Out of labor force")

tun_bjka_na$employ_status <- factor(tun_bjka_na$employ_status,
                                    levels = c("Employed", 
                                               "Unemployed/Out of labor force"))
table(tun_bjka_na$employ_status)


# YouGov: Employment (employ_status)
tun_yg$employ_status <- ifelse(tun_yg$emp_status == "Working", "Employed", "Unemployed/Out of labor force")

tun_yg$employ_status <- factor(tun_yg$employ_status,
                               levels = c("Employed", 
                                          "Unemployed/Out of labor force"))
table(tun_yg$employ_status)

#########################################################################   
#                save data for other analysis                           #   
#########################################################################

write.xlsx(tun_bjka_na,"~/Dropbox/Voters and Female Politicians/Political Behavior/tun_bjka.xlsx")
write.xlsx(tun_yg, "~/Dropbox/Voters and Female Politicians/Political Behavior/tun_yg.xlsx")




